Skip to content

gccrs: Implement rest pattern support for slice patterns #4062

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 17, 2025

Conversation

Polygonalr
Copy link
Contributor

@Polygonalr Polygonalr commented Aug 11, 2025

My meatiest PR to this repo so far. I'm ready to be grilled for my code style (especially in rust-compile-pattern.cc).

Similar to my changes made to the AST for rest-pattern-in-slice-pattern support, the main change is adding SlicePatternItemsNoRest and SlicePatternItemsHasRest classes which hold patterns for HIR::SlicePattern. The changes made to rust-compile-pattern.cc is also quite significant due to having 4 different cases to handle (array parent + no rest, array parent + has rest, slice parent + no rest, slice parent + has rest) - reviewers should focus more on that file, since other changes are mainly cascading changes stemmed from the introduction of new classes.

This patch adds support for compiling rest patterns present in slice patterns
(e.g. `[1, .., 2]`). 006t.original output from compiling match-slicepattern-slice-2.rs
for first 2 match arms:

...
  if (RUSTTMP.3.len > 1 && *NON_LVALUE_EXPR <RUSTTMP.3.data> == 1)
    {
      {
                struct () RUSTTMP.4;
        {
                    struct () RUSTTMP.5;
          {
           ..
          }
        }
        goto <D.139>;
      }
    }
  if ((RUSTTMP.3.len > 1 && *NON_LVALUE_EXPR <RUSTTMP.3.data> == 0) && *(RUSTTMP.3.data + (sizetype) (RUSTTMP.3.len + 18446744073709551615) * 4) == 0)
    {
      {
                struct () RUSTTMP.6;
        {
                    struct () RUSTTMP.7;
          {
          }
        }
        goto <D.139>;
      }
    }
...

gcc/rust/ChangeLog:

	* hir/tree/rust-hir-pattern.h: Add SlicePatternItems base class and
	SlicePatternItemsNoRest/SlicePatternItemsHasRest derived classes.
	Update SlicePattern to hold patterns using the new classes.
	* hir/tree/rust-hir-full-decls.h: Declare new classes.
	* hir/tree/rust-hir.cc: Add visits for new classes.
	* hir/tree/rust-hir-visitor.h: Add visits for new classes.
	* hir/tree/rust-hir-visitor.cc: Implement visits for new classes.
	* hir/rust-hir-dump.h: Add visits for new classes.
	* hir/rust-hir-dump.cc: Implement Dump::visit for new classes.
	* hir/rust-ast-lower-base.h: Declare new lower_slice_pattern_no_rest/has_rest
	methods.
	* hir/rust-ast-lower-base.cc: Implement lower_slice_pattern_no_rest/has_rest
	to lower AST slice pattern items to HIR.
	* hir/rust-ast-lower-pattern.cc: Update ASTLoweringPattern::visit for
	SlicePattern to use new lowering methods.
	* backend/rust-compile-pattern.cc: Update CompilePatternCheckExpr::visit
	and CompilePatternBindings::visit for SlicePattern to handle
	SlicePatternItemsNoRest/HasRest.
	* checks/errors/borrowck/rust-bir-builder-pattern.cc: Update
	PatternBindingBuilder::visit for SlicePattern to iterate members correctly.
	* checks/errors/borrowck/rust-bir-builder-struct.h: Add visits for new
	classes.
	* checks/errors/borrowck/rust-function-collector.h: Add visits for new
	classes.
	* checks/errors/rust-const-checker.h: Add visits for new classes.
	* checks/errors/rust-const-checker.cc: Implement empty visits for new classes.
	* checks/errors/rust-hir-pattern-analysis.h: Add visits for new classes.
	* checks/errors/rust-hir-pattern-analysis.cc: Implement empty visits for new
	classes.
	* checks/errors/rust-unsafe-checker.h: Add visits for new classes.
	* checks/errors/rust-unsafe-checker.cc: Implement empty visits for new
	classes.
	* typecheck/rust-hir-type-check-pattern.cc: Update TypeCheckPattern::visit
	for SlicePattern to handle SlicePatternItemsNoRest/HasRest.

gcc/testsuite/ChangeLog:

	* rust/compile/slice_rest_pattern.rs: Removed -fsyntax-only.

Signed-off-by: Yap Zhi Heng <[email protected]>
@Polygonalr Polygonalr force-pushed the slicepattern-rest-hir-final branch from feed751 to add4aea Compare August 12, 2025 01:23
Copy link
Member

@philberty philberty left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM nice work big patch

@philberty philberty added this pull request to the merge queue Aug 17, 2025
Merged via the queue into Rust-GCC:master with commit 051429a Aug 17, 2025
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants